package com.zhang.controller;

import com.zhang.config.Result;
import com.zhang.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    ArticleMapperImpl articleMapperImpl = new ArticleMapperImpl();

    /**
     * 根据时间查询
     *
     * @param pageNum 页号
     * @param pageSize 单个页面大小
     * @param time 查询限定时间（天）
     * @return
     */
    @RequestMapping("/searchByTime")
    public Result<?> selectAllArticle(@RequestParam(defaultValue = "1") Integer pageNum,
                                      @RequestParam(defaultValue = "14") Integer pageSize,
                                      @RequestParam(defaultValue = "7") Integer time){
         return articleMapperImpl.searchByTime(pageNum, pageSize, time);
    }

    /**
     * 分页与模糊查询
     *
     * @param pageNum
     * @param pageSize
     * @param search
     * @return
     */
    @RequestMapping("/fuzzySearch")
    public Result<?> fuzzySearch(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "14") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        return articleMapperImpl.fuzzySearch(pageNum,pageSize,search);
    }

    /**
     * 每日更新
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateArticle")
    public Result<?> updateArticle() throws Exception {

        return articleMapperImpl.updateArticle();
    }

    /**
     * 初次插入数据
     *
     * @return
     */
    @RequestMapping("/insertFirst")
    public Result<?> insertFirst() {
        return articleMapperImpl.insertFirst();
    }

    @RequestMapping("/searchByID") //根据id查询
    public Result<?> searchByID(@RequestParam Long articleId) {
        if (articleId!=null){
            return articleMapperImpl.searchByID(articleId);
        }else {
            return  Result.error("-1", "id不可为空");
        }
    }
}